/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.inventory.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.inventory.dao.StoreInventoryCheckDao;
import com.chlitina.store.modules.inventory.dao.StoreInventoryCheckDetailDao;
import com.chlitina.store.modules.inventory.entity.StoreInventoryCheck;
import com.chlitina.store.modules.inventory.entity.StoreInventoryCheckDetail;

/**
 * 库存盘点Service
 * @author lvbo
 * @version 2013-11-18
 */
@Component
@Transactional(readOnly = true)
public class StoreInventoryCheckService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreInventoryCheckService.class);
	
	@Autowired
	private StoreInventoryCheckDao storeInventoryCheckDao;
	@Autowired
	private StoreInventoryCheckDetailDao storeInventoryCheckDetailDao;
	
	public StoreInventoryCheck get(Long id) {
		return storeInventoryCheckDao.findOne(id);
	}
	
	public Page<StoreInventoryCheck> find(Page<StoreInventoryCheck> page, StoreInventoryCheck storeInventoryCheck, String startDate, String endDate) {
		DetachedCriteria dc = storeInventoryCheckDao.createDetachedCriteria();
		if (storeInventoryCheck.getStore()!=null && storeInventoryCheck.getStore().getId()>0){
			dc.add(Restrictions.eq("store.id", storeInventoryCheck.getStore().getId()));
		}
		Date start = null;
		Date end = null;
		try {
			start = DateUtils.parseDate(startDate);
			start = DateUtils.getBeginOfDay(start);
			end = DateUtils.parseDate(endDate);
			end = DateUtils.getEndOfDay(end);
		} catch (Exception e) {
//			System.out.println("字符串转换日期异常");
		}
		if (start != null && end == null) {
			dc.add(Restrictions.ge("checkTime", start));
		} else if (start == null && end != null) {
			dc.add(Restrictions.le("checkTime", end));
		} else if (start != null && end != null) {
			dc.add(Restrictions.between("checkTime", start, end));
		}
		dc.addOrder(Order.desc("id"));
		return storeInventoryCheckDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreInventoryCheck storeInventoryCheck) {
		storeInventoryCheckDao.save(storeInventoryCheck);
	}
	@Transactional(readOnly = false)
	public List<StoreInventoryCheckDetail> findDetails(Long id) {
		DetachedCriteria dc = storeInventoryCheckDetailDao.createDetachedCriteria();
		dc.add(Restrictions.eq("storeInventoryCheck.id", id));
		return storeInventoryCheckDetailDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		List<StoreInventoryCheckDetail> checkDetailList = findDetails(id);
		for (StoreInventoryCheckDetail d : checkDetailList) {
			storeInventoryCheckDetailDao.delete(d);
		}
		storeInventoryCheckDao.delete(id);
	}
	
}
